package com._520it.crm.service.impl;

import com._520it.crm.domain.Employee;
import com._520it.crm.domain.Permission;
import com._520it.crm.domain.Role;
import com._520it.crm.mapper.RoleMapper;
import com._520it.crm.page.PageResult;
import com._520it.crm.query.RoleQueryObject;
import com._520it.crm.service.IRoleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Collections;
import java.util.List;
@Service
public class RoleServiceImpl implements IRoleService {
    @Autowired
    RoleMapper roleMapper;

    @Override
    public int deleteByPrimaryKey(Long id) {
        return roleMapper.deleteByPrimaryKey(id);
    }

    @Override
    public int insert(Role record) {
        int count = roleMapper.insert(record);
        for(Permission p :record.getPermissions()){
            roleMapper.insertPermissionRelation(record.getId(),p.getId());

        }
        return count;
    }

    @Override
    public Role selectByPrimaryKey(Long id) {
        return roleMapper.selectByPrimaryKey(id);
    }

    @Override
    public List<Role> selectAll() {
        return roleMapper.selectAll();
    }

    @Override
    public int updateByPrimaryKey(Role record) {
        roleMapper.deletePermissionRelation(record.getId());

        for(Permission p :record.getPermissions()){
            roleMapper.insertPermissionRelation(record.getId(),p.getId());

        }
        return roleMapper.updateByPrimaryKey(record);
    }

    @Override
    public PageResult queryPage(RoleQueryObject qo) {
        Integer totalCount = roleMapper.queryforCount(qo);
        if (totalCount==0){
            return new PageResult(0, Collections.EMPTY_LIST);
        }
        List<Employee> data = roleMapper.queryforList(qo);
        return new PageResult(totalCount,data);
    }

    @Override
    public List<Role> queryRoleIdByEmployeeId(Long employeeId) {
        return roleMapper.queryRoleIdByEmployeeId(employeeId);
    }

    @Override
    public List<String> queryRoleSnByEmpId(Long id) {
        return roleMapper.queryRoleSnByEmpId(id);
    }
}
